package T4.EjemplosDeIO;


import java.net.Socket;
import java.io.*;

public class Ejemplo8 {

    public static void main(String[] args) {
        Socket socket = null;
        DataOutputStream dataOutputStream = null;
        DataInputStream dataInputStream = null;
        OutputStream dutputStream = null;
        try {
            socket = new Socket("127.0.0.1", 8081);
            dutputStream = socket.getOutputStream();
            dataOutputStream = new DataOutputStream(dutputStream);
            dataOutputStream.writeBytes("Hola que tal; yo soy el cliente\n");
            dataOutputStream.writeInt(23);
            InputStream is = socket.getInputStream();
            dataInputStream = new DataInputStream(is);
            System.out.println(dataInputStream.readLine());
            System.out.println(dataInputStream.readFloat());
        } catch (Exception ex) {
            System.out.println("Fallo en la red");
            ex.printStackTrace();
        } finally {
            try {
                if (dataOutputStream != null) {
                    dataOutputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            try {
                if (dataInputStream != null) {
                    dataInputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            try {
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}